﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using BartonExtension;

namespace SqliteWrapper.Procedures
{
    public class Proc_AddAlbum : IProcedure
    {
        public override SqliteError doProcedure(object DataSource, string DBPath)
        {
            Album album = DataSource as Album;
            SqliteError error = new SqliteError() { IsProcessSuccess = false, ErrorText = null };
            SqliteConnectionManager manager = SqliteConnectionManager.Create(DBPath);
            using (SQLiteWriteLock writeLock = new SQLiteWriteLock(manager.ConnectionString))
            {
                using (SQLiteConnection conn = manager.GetSqliteConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "select count(*) from Album where Title = @Title";
                        cmd.Parameters.Add("@Title", System.Data.DbType.String).Value = album.Title;
                        int count = cmd.ExecuteScalar().toInt32().Value;
                        if (count == 0)
                        {
                            cmd.CommandText = "insert into Album(Title,CreateTimeTag,Desc,Thumbnail) values(@Title,@CreateTimeTag,@Desc,@Thumbnail); select last_insert_rowid()";
                            cmd.Parameters.Add("@Title", System.Data.DbType.String).Value = album.Title;
                            cmd.Parameters.Add("@CreateTimeTag", System.Data.DbType.String).Value = album.CreateTimeTag;
                            cmd.Parameters.Add("@Desc", System.Data.DbType.String).Value = album.Desc;
                            cmd.Parameters.Add("@Thumbnail", System.Data.DbType.String).Value = album.Thumbnail;
                            try
                            {
                                var p_key_album = cmd.ExecuteScalar();

                                ////Update Books
                                if (!album.BookList.isNullCollectionOrCount())
                                {
                                    using (SQLiteTransaction trans = conn.BeginTransaction())
                                    {
                                        foreach (Book x in album.BookList)
                                        {
                                            x.AlbumId = Convert.ToInt64(p_key_album);
                                            using (SQLiteCommand subCmd = conn.CreateCommand())
                                            {
                                                subCmd.CommandText = "insert into Book(Title,SourcePath,AlbumId,CategoryId,CreateTimeTag,Desc,Thumbnail) " +
                                                                     "values(@Title,@SourcePath,@AlbumId,@CategoryId,@CreateTimeTag,@Desc,@Thumbnail)";
                                                subCmd.Parameters.Add("@Title", System.Data.DbType.String).Value = x.Title;
                                                subCmd.Parameters.Add("@SourcePath", System.Data.DbType.String).Value = x.SourcePath;
                                                subCmd.Parameters.Add("@AlbumId", System.Data.DbType.Int64).Value = x.AlbumId;
                                                subCmd.Parameters.Add("@CategoryId", System.Data.DbType.Int64).Value = x.CategoryId;
                                                subCmd.Parameters.Add("@CreateTimeTag", System.Data.DbType.String).Value = x.CreateTimeTag;
                                                subCmd.Parameters.Add("@Desc", System.Data.DbType.String).Value = x.Desc;
                                                subCmd.Parameters.Add("@Thumbnail", System.Data.DbType.String).Value = x.Thumbnail;
                                                subCmd.Transaction = trans;
                                                subCmd.ExecuteNonQuery();
                                            }
                                        }
                                        try
                                        {
                                            trans.Commit();
                                            error.IsProcessSuccess = true;
                                        }
                                        catch (Exception ex)
                                        {
                                            error.ErrorText = ex.Message;
                                            trans.Rollback();
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                error.ErrorText = ex.Message;
                            }
                        }
                        else
                        {
                            error.ErrorText = "系列已存在";
                            error.IsProcessSuccess = false;
                        }
                    }
                }
            }
            return error;
        }
    }
}
